// SettingAGVDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DSTapeRemove.h"
#include "DSTapeRemoveDoc.h"
#include "SettingAGVDlg.h"

#include "IO/IIO.h"
#include "IO/Io_def.h"
#include "Util/MinUtilExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSettingAGVDlg dialog


CSettingAGVDlg::CSettingAGVDlg(IIO *pIO, CWnd* pParent /*=NULL*/)
	: CDialog(CSettingAGVDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSettingAGVDlg)
		// NOTE: the ClassWizard will add member initialization here
	m_lnkIO = pIO;
	m_TimerInitFlag = FALSE;
	//}}AFX_DATA_INIT
}


void CSettingAGVDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSettingAGVDlg)
	DDX_Control(pDX, IDB_INPUT1, m_bInput[0]);
	DDX_Control(pDX, IDB_INPUT2, m_bInput[1]);
	DDX_Control(pDX, IDB_INPUT3, m_bInput[2]);
	DDX_Control(pDX, IDB_INPUT4, m_bInput[3]);
	DDX_Control(pDX, IDB_INPUT5, m_bInput[4]);
	DDX_Control(pDX, IDB_INPUT6, m_bInput[5]);
	DDX_Control(pDX, IDB_INPUT7, m_bInput[6]);
	DDX_Control(pDX, IDB_INPUT8, m_bInput[7]);

	DDX_Control(pDX, IDB_OUTPUT1, m_bOutput[0]);
	DDX_Control(pDX, IDB_OUTPUT2, m_bOutput[1]);
	DDX_Control(pDX, IDB_OUTPUT3, m_bOutput[2]);
	DDX_Control(pDX, IDB_OUTPUT4, m_bOutput[3]);
	DDX_Control(pDX, IDB_OUTPUT5, m_bOutput[4]);
	DDX_Control(pDX, IDB_OUTPUT6, m_bOutput[5]);
	DDX_Control(pDX, IDB_OUTPUT7, m_bOutput[6]);
	DDX_Control(pDX, IDB_OUTPUT8, m_bOutput[7]);

	DDX_Control(pDX, IDB_EXIT, m_bExit);
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSettingAGVDlg, CDialog)
	//{{AFX_MSG_MAP(CSettingAGVDlg)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDB_OUTPUT1, OnOutput1)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDB_OUTPUT2, OnOutput2)
	ON_BN_CLICKED(IDB_OUTPUT3, OnOutput3)
	ON_BN_CLICKED(IDB_OUTPUT4, OnOutput4)
	ON_BN_CLICKED(IDB_OUTPUT5, OnOutput5)
	ON_BN_CLICKED(IDB_OUTPUT6, OnOutput6)
	ON_BN_CLICKED(IDB_OUTPUT7, OnOutput7)
	ON_BN_CLICKED(IDB_OUTPUT8, OnOutput8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSettingAGVDlg message handlers

void CSettingAGVDlg::ButtonInitial()
{
	m_bExit.SetIcon(IDI_EXIT, 32, 32);
	m_bExit.SetImagePos(CPoint(MINBUTTON_CENTER,5));
	m_bExit.SetTextPos(CPoint(MINBUTTON_CENTER,40));
	m_bExit.SetTextSize(10,TRUE);

}

BOOL CSettingAGVDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	int i;
	for(i=0;i<8;i++) 
	{
		m_nInSts[i] = 0;
		m_nInOldSts[i] = -1;
		m_nOutSts[i] = 0;
		m_nOutOldSts[i] = -1;
	}
	m_nTimerID = SetTimer(1, 100, NULL);
	if( !m_nTimerID )
	{
		MyMessageBox("Timer has not been created.","Windows Error", M_ICONERROR);
		m_TimerInitFlag = FALSE;
	}
	else m_TimerInitFlag = TRUE;

	ButtonInitial();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSettingAGVDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CSettingAGVDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int i;

	for(i= 0;i<8;i++)
	{
		m_nInSts[i] = m_lnkIO->IsOn(X_PIO_INPUT_1+i);
		m_nOutSts[i] = m_lnkIO->IsOn(Y_PIO_OUTPUT_1+i);
	}

	for(i=0;i<8;i++)
	{
		if( m_nInSts[i] != m_nInOldSts[i] )
		{
			m_nInOldSts[i] = m_nInSts[i];
			if(m_nInSts[i])
			{
				m_bInput[i].SetColor(YELLOW);
				m_bInput[i].SetColorTab(YELLOW);
			}
			else
			{
				m_bInput[i].SetColor(LTGRAY);
				m_bInput[i].SetColorTab(LTGRAY);
			}
		}
		
		if( m_nOutSts[i] != m_nOutOldSts[i] )
		{
			m_nOutOldSts[i] = m_nOutSts[i];
			if(m_nOutSts[i])
			{
				m_bOutput[i].SetColor(YELLOW);
				m_bOutput[i].SetColorTab(YELLOW);
			}
			else
			{
				m_bOutput[i].SetColor(LTGRAY);
				m_bOutput[i].SetColorTab(LTGRAY);
			}
		}
		
	}
	CDialog::OnTimer(nIDEvent);
	
}

void CSettingAGVDlg::OnOutput1() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_1);
}

void CSettingAGVDlg::OnOutput2() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_2);
}

void CSettingAGVDlg::OnOutput3() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_3);	
}

void CSettingAGVDlg::OnOutput4() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_4);	
}

void CSettingAGVDlg::OnOutput5() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_5);	
}

void CSettingAGVDlg::OnOutput6() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_6);	
}

void CSettingAGVDlg::OnOutput7() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_7);	
}

void CSettingAGVDlg::OnOutput8() 
{
	// TODO: Add your control notification handler code here
	m_lnkIO->OutputToggle(Y_PIO_OUTPUT_8);	
}

void CSettingAGVDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	if(m_TimerInitFlag)
		KillTimer(m_nTimerID);
	m_TimerInitFlag = FALSE;
	// TODO: Add your message handler code here
	
}
